*!Version 1.0 Aug13
program define graphsubqte, sortpreserve

*** Program to do graph for a qte

*** adapted from *!Version 4.0 April 12 f graphsharecdf-4.0.ado
***	Uses version 12 graphics
***
***	Puts graphs in `path' directory
***     Labels legends using `legnd1' through `legnd3'
***
*** This has the shares for each subgroup sum to one over the while CDF
*** Then, figures out where the thing is constant for the 0 earners, and shows last 5 percentiles
*** of that, along with other measures


syntax varlist(min=2 max=2) [if] [in] [aweight/], /*
	*/ VARS(string) NAMEGraph(string) PATH(string) TITL(string) [LEGND1(string)] [LEGND2(string)] [LEGND3(string)]

	/* parse varlist */
	local depvar : word 1 of `varlist'
	local treat  : word 2 of `varlist'

	marksample touse 

	keep if `touse'  

	parse "vars", parse( " ") 
	preserve

	/* giving weight var a mnemonic name */
	if trim("`weight'") ~= "" { 
		local weightexp "`exp'" 
		local weight = "[aw =`weightexp' ]" 
		di "weight is `weigh'"
  		} /*only want to do this once*/

*qui { /* start of quietly loop */

	local numvars: word count `vars'
	di "number of vars is : `numvars'"
	di "weight is: `weight'
	
	* get vars
	local vQ1 : word 1 of `vars'
	local vQ2 : word 2 of `vars'
	if `numvars' == 3 {
		local vQ3 : word 3 of `vars'
		}

	di "`vQ1' name is `legnd1'"

	capture drop centile
	gen centile = _n if _n <=99
	
	* set graph so logo is off while still in stata 10
	graph set ps logo off

	*** within subgroup
	if `numvars'==3 {	

	   qte `depvar' `treat' if `vQ1'==1 `weight', q(1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99) nokd  nojcumul
	   mat qtevQ1 = e(bd)'
	   svmat double qtevQ1

	   qte `depvar' `treat' if `vQ2'==1 `weight', q(1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99) nokd  nojcumul
	   mat qtevQ2 = e(bd)'
	   svmat double qtevQ2

	   qte `depvar' `treat' if `vQ3'==1 `weight', q(1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99) nokd  nojcumul
	   mat qtevQ3 = e(bd)'
	   svmat double qtevQ3

	   label variable centile "Percentile index"
	   sort centile

	   di "color `namegraph' 1"
	   * do color versions
	   ** set scheme (s1 color, white )
	   set scheme s1color

	   * graph qtes only do once for color and bw
	   scatter qtevQ1 qtevQ2 qtevQ3 centile if centile >= 1 & centile <= 98, sort connect(l l l)  lcolor(blue green red) plotregion(style(none)) graphregion(fcolor(white) lstyle(none) ilstyle(none) lcolor(white) ilcolor(white)) legend(region(lstyle(none) ) lcolor(white) label(1 "`legnd1'") label(2 "`legnd2'") label(3 "`legnd3'") rows(2)) lpattern(l - .)  msymbol(i i i) saving(`path'/`namegraph'.gph, replace) title("") b1("")  xlab(10 20 30 40 50 60 70 80 90) ylab(-800 -400 0 400 800   , angle(horizontal)) yline(0)
	graph export `path'/`namegraph'.ps, replace logo(off)
	graph export `path'/`namegraph'.eps, replace logo(off)
	!perl -pi -e 's/w setlinewidth/w 3 mul setlinewidth/' `path'/`namegraph'.ps
	!perl -pi -e 's/w setlinewidth/w 3 mul setlinewidth/' `path'/`namegraph'.eps
	!ps2pdf `path'/`namegraph'.ps `path'/`namegraph'.pdf

	di "black and white `namegraph' 1"
	* do black and white versions
	** set scheme (s1 mono, white no background)
	set scheme s1mono


	scatter qtevQ1 qtevQ2 qtevQ3 centile if centile >= 1 & centile <= 98, sort connect(l l l ) plotregion(style(none)) graphregion(fcolor(white) lstyle(none) ilstyle(none) lcolor(white) ilcolor(white)) legend(region(lstyle(none) ) lcolor(white) label(1 "`legnd1'") label(2 "`legnd2'") label(3 "`legnd3'") rows(2)) lpattern(l - .) msymbol(i i i) saving(`path'/`namegraph'-bw.gph, replace) title("")  b1("") xlab(10 20 30 40 50 60 70 80 90) ylab(-800 -400 0 400 800   , angle(horizontal))   yline(0)
	graph export `path'/`namegraph'-bw.ps, replace logo(off)
	graph export `path'/`namegraph'-bw.eps, replace logo(off)
	!perl -pi -e 's/w setlinewidth/w 3 mul setlinewidth/' `path'/`namegraph'-bw.ps
	!perl -pi -e 's/w setlinewidth/w 3 mul setlinewidth/' `path'/`namegraph'-bw.eps
	!ps2pdf `path'/`namegraph'-bw.ps `path'/`namegraph'-bw.pdf


	} /* end of loop for 3 vars */

	*** for 2 variables
	else if `numvars'==2 { /* start of loop if 2 variables */
	   qte `depvar' `treat' if `vQ1'==1 `weight', q(1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99) nokd  nojcumul
	   mat qtevQ1 = e(bd)'
	   svmat double qtevQ1

	   qte `depvar' `treat' if `vQ2'==1 `weight', q(1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99) nokd  nojcumul
	   mat qtevQ2 = e(bd)'
	   svmat double qtevQ2

	   label variable centile "Percentile index"
	   sort centile

	   di "color `namegraph' 1"
	   * do color versions
	   ** set scheme (s1 color, white )
	   set scheme s1color

	   * graph qtes only do once for color and bw
	   scatter qtevQ1 qtevQ2 centile if centile >= 1 & centile <= 98, sort connect(l l )  lcolor(blue green) plotregion(style(none)) graphregion(fcolor(white) lstyle(none) ilstyle(none) lcolor(white) ilcolor(white)) legend(region(lstyle(none) ) lcolor(white) label(1 "`legnd1'") label(2 "`legnd2'") rows(2)) lpattern(l - )  msymbol(i i) saving(`path'/`namegraph'.gph, replace) title("") b1("")  xlab(10 20 30 40 50 60 70 80 90) ylab(-800 -400 0 400 800   , angle(horizontal)) yline(0)
	graph export `path'/`namegraph'.ps, replace logo(off)
	graph export `path'/`namegraph'.eps, replace logo(off)
	!perl -pi -e 's/w setlinewidth/w 3 mul setlinewidth/' `path'/`namegraph'.ps
	!perl -pi -e 's/w setlinewidth/w 3 mul setlinewidth/' `path'/`namegraph'.eps
	!ps2pdf `path'/`namegraph'.ps `path'/`namegraph'.pdf

	di "black and white `namegraph' 1"
	* do black and white versions
	** set scheme (s1 mono, white no background)
	set scheme s1mono

	scatter qtevQ1 qtevQ2 centile if centile >= 1 & centile <= 98, sort connect(l l ) plotregion(style(none)) graphregion(fcolor(white) lstyle(none) ilstyle(none) lcolor(white) ilcolor(white)) legend(region(lstyle(none) ) lcolor(white) label(1 "`legnd1'") label(2 "`legnd2'") rows(2)) lpattern(l -) msymbol(i i ) saving(`path'/`namegraph'-bw.gph, replace) title("")  b1("") xlab(10 20 30 40 50 60 70 80 90) ylab(-800 -400 0 400 800   , angle(horizontal))   yline(0)
	graph export `path'/`namegraph'-bw.ps, replace logo(off)
	graph export `path'/`namegraph'-bw.eps, replace logo(off)
	!perl -pi -e 's/w setlinewidth/w 3 mul setlinewidth/' `path'/`namegraph'-bw.ps
	!perl -pi -e 's/w setlinewidth/w 3 mul setlinewidth/' `path'/`namegraph'-bw.eps
	!ps2pdf `path'/`namegraph'-bw.ps `path'/`namegraph'-bw.pdf


	} /* end of loop for 2 vars */

*}	/* end of quietly loop */
*	version 12

*	pause on
*	pause

	restore


end
	
